//
//  HomeMenuCell.m
//  meituan
//
//  Created by jinzelu on 15/6/30.
//  Copyright (c) 2015年 jinzelu. All rights reserved.
//

#import "HomeMenuCell.h"
#import "HLTfenleiTableViewController.h"
#import "HLTzhuanzhangTableViewController.h"
#import "HLTmyuserinfoTableViewController.h"
#import "HLTuserchargeViewController.h"
#import "HLTgonghuiuserViewController.h"
@interface HomeMenuCell ()<UIScrollViewDelegate>
{
    UIView *_backView1;
    UIView *_backView2;
    UIPageControl *_pageControl;
}

@end

@implementation HomeMenuCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier menuArray:(NSMutableArray *)menuArray{
    self.menulist=menuArray;
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //
        _backView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screen_width, 160)];
        _backView2 = [[UIView alloc] initWithFrame:CGRectMake(screen_width, 0, screen_width, 160)];
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screen_width, 180)];
        scrollView.contentSize = CGSizeMake(screen_width, 180);
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        scrollView.showsHorizontalScrollIndicator = NO;
        
        [scrollView addSubview:_backView1];
//        [scrollView addSubview:_backView2];
        [self addSubview:scrollView];
        
        //创建8个
        for (int i = 0; i < 8; i++) {
            if (i < 4) {
                CGRect frame = CGRectMake(i*screen_width/4, 0, screen_width/4, 80);
                NSString *title = [menuArray[i] objectForKey:@"title"];
                NSString *imageStr = [menuArray[i] objectForKey:@"image"];
                JZMTBtnView *btnView = [[JZMTBtnView alloc] initWithFrame:frame title:title imageStr:imageStr];
                btnView.tag = 10+i;
                [_backView1 addSubview:btnView];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
                [btnView addGestureRecognizer:tap];
                
            }else if(i<8){
                CGRect frame = CGRectMake((i-4)*screen_width/4, 80, screen_width/4, 80);
                NSString *title = [menuArray[i] objectForKey:@"title"];
                NSString *imageStr = [menuArray[i] objectForKey:@"image"];
                JZMTBtnView *btnView = [[JZMTBtnView alloc] initWithFrame:frame title:title imageStr:imageStr];
                btnView.tag = 10+i;
                [_backView1 addSubview:btnView];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
                [btnView addGestureRecognizer:tap];
            }else if(i < 12){
                CGRect frame = CGRectMake((i-8)*screen_width/4, 0, screen_width/4, 80);
                NSString *title = [menuArray[i] objectForKey:@"title"];
                NSString *imageStr = [menuArray[i] objectForKey:@"image"];
                JZMTBtnView *btnView = [[JZMTBtnView alloc] initWithFrame:frame title:title imageStr:imageStr];
                btnView.tag = 10+i;
                [_backView2 addSubview:btnView];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
                [btnView addGestureRecognizer:tap];
            }else{
                CGRect frame = CGRectMake((i-12)*screen_width/4, 80, screen_width/4, 80);
                NSString *title = [menuArray[i] objectForKey:@"title"];
                NSString *imageStr = [menuArray[i] objectForKey:@"image"];
                JZMTBtnView *btnView = [[JZMTBtnView alloc] initWithFrame:frame title:title imageStr:imageStr];
                btnView.tag = 10+i;
                [_backView2 addSubview:btnView];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
                [btnView addGestureRecognizer:tap];
            }
        }
        
        //
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(screen_width/2-20, 160, 0, 20)];
        _pageControl.currentPage = 0;
        _pageControl.numberOfPages = 0;
//        self.backgroundColor = [UIColor redColor];
        [self addSubview:_pageControl];
        [_pageControl setCurrentPageIndicatorTintColor:navigationBarColor];
        [_pageControl setPageIndicatorTintColor:[UIColor grayColor]];
        
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)OnTapBtnView:(UITapGestureRecognizer *)sender{
    NSLog(@"tag:%ld",sender.view.tag);
    
    
    
    if (sender.view.tag<15) {
        HLTfenleiTableViewController *shopvc=[[HLTfenleiTableViewController alloc]init];
        //隐藏tabbar
        shopvc.hidesBottomBarWhenPushed = YES;
        shopvc.categary=_menulist[sender.view.tag-10][@"title"];
        
        shopvc.navigationItem.title=_menulist[sender.view.tag-10][@"title"];
        [self.parent.navigationController pushViewController:shopvc animated:YES];
        
    }
    else if (sender.view.tag==15)
    {
        Account *acc=[[AccountTool sharedAccountTool]currentAccount];
        if (![acc.pwd isEqual:@""]) {
            Account *acc=[[AccountTool sharedAccountTool] currentAccount];
            //
            NSString *url=[NSString stringWithFormat:@"%@&user_name=%@",usercharge,acc.custPhone];
            
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            HLTuserchargeViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTuserchargeViewController"];
            vc.hidesBottomBarWhenPushed = YES;
            vc.url=url;
            //            [vc setmywebview];
            
            //            HLTCustomerserviceTableViewController *vc =[[HLTCustomerserviceTableViewController alloc]init];
            [self.parent.navigationController pushViewController:vc animated:NO];
            
        }else{
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"亲!你还没有登陆哟-_- " delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
         
            [alert show];
        }
        
        
        
        
       
        
    }
    else if (sender.view.tag==16)
    {
        
        
        
        Account *acc=[[AccountTool sharedAccountTool]currentAccount];
        if (![acc.pwd isEqual:@""]) {
            
            
            
            UIView *view1=[[UIView alloc] initWithFrame:self.parent.view.bounds];
            view1.backgroundColor=[UIColor grayColor];
            view1.alpha=0.4;
            [self.parent.view addSubview:view1];
            MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
            indicatorView.delegate = self;
            indicatorView.numberOfCircles = 3;
            indicatorView.radius = 20;
            indicatorView.internalSpacing = 3;
            indicatorView.center = self.parent.view.center;
            [indicatorView startAnimating];
            [self.parent.view addSubview:indicatorView];
            
            Account *acc=[[AccountTool sharedAccountTool] currentAccount];
            
            
            NSString *url=[chaxungonghui stringByAppendingFormat:@"&username=%@&card_num=%@",acc.custPhone,acc.cardNo];
            //将url转换一下格式，否则请求的格式就不兼容
            //    url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
            [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
                
                [indicatorView stopAnimating];
                [view1 removeFromSuperview];
                
                
                
                
                if (ary[@"name"]) {
                    
                    HLTgonghuiuserViewController *vc=[[HLTgonghuiuserViewController alloc]init];
                    vc.hidesBottomBarWhenPushed = YES;
                    [self.parent.navigationController pushViewController:vc animated:NO];
                
                    
                }
                else
                {
                    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:ary[@"msg"] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
                    [alert show];
                    
                }
                
                
                
            } faliure:^(NSString *error) {
                [indicatorView stopAnimating];
                [view1 removeFromSuperview];
                
                
            }];

            
        }else{
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"亲!你还没有登陆哟-_- " delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
            
            [alert show];
        }

        
        
        
        
    }else if (sender.view.tag==17)
    {
        
        Account *acc=[[AccountTool sharedAccountTool]currentAccount];
        if (![acc.pwd isEqual:@""]) {
            UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            HLTmyuserinfoTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTmyuserinfoTableViewController"];
            vc.hidesBottomBarWhenPushed = YES;
            
            [self.parent.navigationController pushViewController:vc animated:NO];
            
        }else{
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"亲!你还没有登陆哟-_- " delegate:nil cancelButtonTitle:nil otherButtonTitles:@"好", nil];
            
            [alert show];
        }
    }
    
   
    
}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat scrollViewW = scrollView.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    int page = (x + scrollViewW/2)/scrollViewW;
    _pageControl.currentPage = page;
}





#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}




@end
